package com.epalmpay.service.mall.impl;

import com.alibaba.fastjson.JSON;
import com.epalmpay.dto.system.ShopListOutDTO;
import com.epalmpay.dto.system.ShopOutDTO;
import com.epalmpay.dto.system.ShopSelectQueryDTO;
import com.epalmpay.service.app.IAppNoticeService;
import com.epalmpay.service.shop.IShopService;
import com.epalmpay.status.Rescode;
import com.epalmpay.bean.MallCartBean;
import com.epalmpay.dto.mall.*;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.mapper.*;
import com.epalmpay.service.mall.IMallGoodService;
import com.epalmpay.service.management.ITemplateMailSendService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Response;
import com.epalmpay.util.Tools;
import com.epalmpay.util.SysUtils;
import javafx.scene.layout.BackgroundImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lifayi on 2017/5/20.
 */
@Service
public class MallGoodServiceImpl implements IMallGoodService {

    @Autowired
    private MallGoodMapper mallGoodMapper;
    @Autowired
    private MallEntityGoodMapper mallEntityGoodMapper;
    @Autowired
    private MallEntityGoodSpecMapper mallEntityGoodSpecMapper;
    @Resource
    private MallFreightMapper mallFreightMapper;
    @Autowired
    private MallCartMapper mallCartMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderGoodMapper orderGoodMapper;
    @Autowired
    private OrderGoodItemMapper orderGoodItemMapper;
    @Autowired
    private TradeOfflineMapper tradeOfflineMapper;
    @Autowired
    private MemberAddressMapper memberAddressMapper;
    @Resource
    private CouponUserMapper couponUserMapper;
    @Resource
    private BonusVerifyLogMapper bonusVerifyLogMapper;
    @Resource
    private MallCategoryMapper mallCategoryMapper;
    @Autowired
    @Lazy
	private ITemplateMailSendService templateMailSendService;

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private IAppNoticeService appNoticeService;
    @Resource
    private IShopService shopService;

    @Override
    public BaseOutDTO addMallGood(MallGoodInDTO mallGoodInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        MallGood mallGood = new MallGood();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(mallGoodInDTO.getValidDateString() != null) {
            mallGood.setValidDate(dateFormat.parse(mallGoodInDTO.getValidDateString()));
        }
        PropertyCopyUtil.copyProperties(mallGoodInDTO, mallGood);
        mallGood.setGroupId(mallGoodInDTO.getGroupId());

        int i = 0;
        if(mallGood.getId() == null) {
            mallGood.setGmtCreate(new Date());
            i = mallGoodMapper.insertSelective(mallGood);
        } else {
            mallGood.setGmtModified(new Date());
            i = mallGoodMapper.updateByPrimaryKeySelective(mallGood);
            mallGoodMapper.updateCategory(mallGood);
        }
        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }

        return baseOutDTO;
    }

    @Override
    public List<MallGood> listMallGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws ParseException {
        MallGood mallGood = new MallGood();
        mallGood.setName(mallGoodQueryDTO.getName());
        mallGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallGood.setGroupId(mallGoodQueryDTO.getGroupId());
        mallGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallGood.setAuditStatus(mallGoodQueryDTO.getAuditStatus());
        mallGood.setShopId(mallGoodQueryDTO.getShopId());
        mallGood.setPage(page);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        return mallGoodMapper.listPageMallGood(mallGood);
    }


    @Override
    public List<MallGood> listAgentMallGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws ParseException {
        MallGood mallGood = new MallGood();
        mallGood.setName(mallGoodQueryDTO.getName());
        mallGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallGood.setGroupId(mallGoodQueryDTO.getGroupId());
        mallGood.setPage(page);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        return mallGoodMapper.listPageAgentMallGood(mallGood);
    }


    @Override
    public MallGoodListOutDTO listEntityGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();

        MallEntityGood mallEntityGood = new MallEntityGood();
        mallEntityGood.setName(mallGoodQueryDTO.getName());
        mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallEntityGood.setGroupId(mallGoodQueryDTO.getGroupId());
        mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallEntityGood.setPage(page);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallEntityGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallEntityGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.listPageMallEntityGood(mallEntityGood);

        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
        for(int i = 0; i < mallEntityGoodList.size(); i++) {
            MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
            PropertyCopyUtil.copyProperties(mallEntityGoodList.get(i), mallGoodOutDTO);

            Map stockNumber=orderGoodMapper.selectAllStockNumberByGoodId(mallGoodOutDTO.getId());
            if(stockNumber==null){
                mallGoodOutDTO.setAllStockNumber(mallGoodOutDTO.getStockNumber()+mallGoodOutDTO.getSaleNum());
            }else{
                mallGoodOutDTO.setAllStockNumber(Integer.parseInt(stockNumber.get("allStockNumber").toString())+mallGoodOutDTO.getStockNumber());
            }

            mallGoodOutDTOList.add(mallGoodOutDTO);
        }

        mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
        mallGoodListOutDTO.setPage(page);
        mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);

        return mallGoodListOutDTO;
    }



    @Override
    public MallGoodListOutDTO listShopEntityGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();

        MallEntityGood mallEntityGood = new MallEntityGood();
        mallEntityGood.setName(mallGoodQueryDTO.getName());
        mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallEntityGood.setPage(page);
        mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallEntityGood.setShopId(mallGoodQueryDTO.getShopId());
        mallEntityGood.setCheckStatus((int)(byte)mallGoodQueryDTO.getAuditStatus());
        mallEntityGood.setGroupId(mallGoodQueryDTO.getGroupId());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallEntityGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallEntityGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.listPageShopMallEntityGood(mallEntityGood);

        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
        for(int i = 0; i < mallEntityGoodList.size(); i++) {
            MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
            PropertyCopyUtil.copyProperties(mallEntityGoodList.get(i), mallGoodOutDTO);
            Map stockNumber=orderGoodMapper.selectAllStockNumberByGoodId(mallGoodOutDTO.getId());
            if(stockNumber==null){
                mallGoodOutDTO.setAllStockNumber(mallGoodOutDTO.getStockNumber()+mallGoodOutDTO.getSaleNum());
            }else{
                mallGoodOutDTO.setAllStockNumber(Integer.parseInt(stockNumber.get("allStockNumber").toString())+mallGoodOutDTO.getStockNumber());
            }
            mallGoodOutDTOList.add(mallGoodOutDTO);
        }

        mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
        mallGoodListOutDTO.setPage(page);
        mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);

        return mallGoodListOutDTO;
    }

    @Override
    public MallGoodListOutDTO listShopAuditEntityGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();

        MallEntityGood mallEntityGood = new MallEntityGood();
        mallEntityGood.setName(mallGoodQueryDTO.getName());
        mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallEntityGood.setPage(page);
        mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallEntityGood.setShopId(mallGoodQueryDTO.getShopId());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallEntityGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallEntityGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.listPageShopAuditMallEntityGood(mallEntityGood);

        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
        for(int i = 0; i < mallEntityGoodList.size(); i++) {
            MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
            PropertyCopyUtil.copyProperties(mallEntityGoodList.get(i), mallGoodOutDTO);
            mallGoodOutDTOList.add(mallGoodOutDTO);
        }

        mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
        mallGoodListOutDTO.setPage(page);
        mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);

        return mallGoodListOutDTO;
    }


    @Override
    public List<MallGood> listShopMallGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws ParseException {
        MallGood mallGood = new MallGood();
        mallGood.setName(mallGoodQueryDTO.getName());
        mallGood.setGroupId(mallGoodQueryDTO.getGroupId());
        mallGood.setMshopId(mallGoodQueryDTO.getShopId());
        mallGood.setPage(page);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        return mallGoodMapper.listPageShopMallGood(mallGood);
    }

    @Override
    public List<MallGood> listAuditShopMallGood(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws ParseException {
        MallGood mallGood = new MallGood();
        mallGood.setName(mallGoodQueryDTO.getName());
        mallGood.setGroupId(mallGoodQueryDTO.getGroupId());
        mallGood.setMshopId(mallGoodQueryDTO.getShopId());
        mallGood.setAuditStatus(mallGoodQueryDTO.getAuditStatus());
        mallGood.setPage(page);

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        return mallGoodMapper.listPageAuditShopMallGood(mallGood);
    }

    @Override
    public BaseOutDTO updateShopMallGood(MallGoodUpdateDTO mallGoodUpdateDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        MallGood mallGood = new MallGood();
        PropertyCopyUtil.copyPropertiesExclude(mallGoodUpdateDTO, mallGood, "shopId");
        mallGood.setMshopId(mallGoodUpdateDTO.getShopId());
        int i = mallGoodMapper.updateByPrimaryKeySelective2(mallGood);
        if(i == 1) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public void updateMallGood(List<MallGoodUpdateDTO> mallGoodUpdateDTOList) throws Exception {
        for(int i = 0; i < mallGoodUpdateDTOList.size(); i++) {
            MallGood mallGood = new MallGood();
            PropertyCopyUtil.copyProperties(mallGoodUpdateDTOList.get(i), mallGood);
            if(mallGood.getCategoryFirstId() != null) {
                mallGoodMapper.updateCategory(mallGood);
            } else {
                mallGoodMapper.updateByPrimaryKeySelective1(mallGood);
            }
        }
    }

    /**
     * 原来的物理删除改为逻辑删除
     * @param items
     */
    @Override
    public void delMallGood(List<Long> items) {
        //mallGoodMapper.delMallGood(items);
        mallGoodMapper.logicDelMallGood(items);//逻辑删除
    }

    @Override
    public MallGoodInDTO getMallGood(Long id) throws Exception {
        MallGoodInDTO mallGoodInDTO = new MallGoodInDTO();
        MallGood mallGood = mallGoodMapper.selectByPrimaryKey(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(mallGood != null) {
            PropertyCopyUtil.copyProperties(mallGood, mallGoodInDTO);
            if(mallGood.getValidDate()!=null){
                mallGoodInDTO.setValidDateString(simpleDateFormat.format(mallGood.getValidDate()));
            }
        }
        return mallGoodInDTO;
    }

    @Override
    public List<Map<String, Object>> getShopList(Long groupId) {
        return mallGoodMapper.listHaveGoodShop(groupId);
    }


    @Override
    public MallGoodSelectiveListOutDTO goodSelective(MallGoodSelectiveQueryDTO mallGoodSelectiveQueryDTO) throws Exception {
        MallGoodSelectiveListOutDTO mallGoodSelectiveListOutDTO = new MallGoodSelectiveListOutDTO();
        List<MallGoodSelectiveOutDTO> mallGoodSelectiveOutDTOList = new ArrayList<MallGoodSelectiveOutDTO>();

        Long total;
        List<MallGood> mallGoodList;
        if(mallGoodSelectiveQueryDTO.getGoodType() == CommonEnum.GoodType.Service.getType()) {
            total = mallGoodMapper.countGoodSelective(mallGoodSelectiveQueryDTO);
            mallGoodList = mallGoodMapper.goodSelective(mallGoodSelectiveQueryDTO);
        } else {
            total = mallEntityGoodMapper.countGoodSelective(mallGoodSelectiveQueryDTO);
            mallGoodList = mallEntityGoodMapper.goodSelective(mallGoodSelectiveQueryDTO);   //这里为了方便  强行使用MallGood作为商城商品的返回值
        }

        for(int i = 0; i < mallGoodList.size(); i++) {
            MallGoodSelectiveOutDTO mallGoodSelectiveOutDTO = new MallGoodSelectiveOutDTO();
            PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodSelectiveOutDTO);
            mallGoodSelectiveOutDTOList.add(mallGoodSelectiveOutDTO);
        }
        mallGoodSelectiveListOutDTO.setTotal(total);
        mallGoodSelectiveListOutDTO.setMallGoodSelectiveOutDTOList(mallGoodSelectiveOutDTOList);
        return mallGoodSelectiveListOutDTO;
    }

    @Override
    public List<Long> getGoodIdByCategoryId(Map<String, List<Long>> items) {
        List<Long> ids = new ArrayList<Long>();
        if(items.get("categoryOneLevelIds").size() > 0) {
            Byte bigGoodType = mallCategoryMapper.selectByPrimaryKey(items.get("categoryOneLevelIds").get(0)).getGoodBigtype();
            if(bigGoodType == CommonEnum.GoodType.Service.getType()) {
                ids.addAll(mallGoodMapper.listIdByOneLevelCatetoryId(items.get("categoryOneLevelIds")));
            } else {
                ids.addAll(mallEntityGoodMapper.listIdByOneLevelCatetoryId(items.get("categoryOneLevelIds")));
            }

        }
        if(items.get("categoryTwoLevelIds").size() > 0) {
            Byte bigGoodType = mallCategoryMapper.selectByPrimaryKey(items.get("categoryTwoLevelIds").get(0)).getGoodBigtype();
            if(bigGoodType == CommonEnum.GoodType.Service.getType()) {
                ids.addAll(mallGoodMapper.listIdByTwoLevelCatetoryId(items.get("categoryTwoLevelIds")));
            } else {
                ids.addAll(mallEntityGoodMapper.listIdByTwoLevelCatetoryId(items.get("categoryTwoLevelIds")));
            }
        }
        return ids;
     }

    @Override
    public List<Map> getGoodListById(List<Long> goodIdList) {
        return mallGoodMapper.getGoodListById(goodIdList);
    }

    @Override
    @Transactional
    public BaseOutDTO saveEntityGoodAdd(EntityGoodInDTO entityGoodInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        int i;
        Date date = new Date();

        MallEntityGood mallEntityGood = new MallEntityGood();
        PropertyCopyUtil.copyProperties(entityGoodInDTO, mallEntityGood);
        mallEntityGood.setGmtModified(date);
        if(mallEntityGood.getId() != null) {
            i = mallEntityGoodMapper.updateByPrimaryKeySelective(mallEntityGood);
            mallEntityGoodMapper.updateCategory(mallEntityGood);
            if(i == 1) {
                //不再物理删除商品规格，改为逻辑删除
                //mallEntityGoodSpecMapper.deleteByGoodId(mallEntityGood.getId());
                mallEntityGoodSpecMapper.logicDelAloneGoodSpec(mallEntityGood.getId());
            }
        } else {
            mallEntityGood.setGmtCreate(date);
            i = mallEntityGoodMapper.insertSelective(mallEntityGood);
        }

        if(i == 1 && entityGoodInDTO.getMallEntityGoodSpecList() != null && entityGoodInDTO.getMallEntityGoodSpecList().size() > 0) {
            mallEntityGoodSpecMapper.insertBatch(entityGoodInDTO.getMallEntityGoodSpecList(), mallEntityGood.getId());
        }

        baseOutDTO.setErrorCode(Rescode.SUCCESS);
        return baseOutDTO;
    }


    @Override
    public BaseOutDTO saveServiceMallGood(MallGoodInDTO mallGoodInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i;
        Date date = new Date();
        MallGood mallGood = new MallGood();
        PropertyCopyUtil.copyProperties(mallGoodInDTO, mallGood);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date vaildDate = sdf.parse(mallGoodInDTO.getValidDateString());
        mallGood.setValidDate(vaildDate);
        mallGood.setGmtModified(date);
        if(mallGoodInDTO.getId() != null) {
            i = mallGoodMapper.updateByPrimaryKeySelective(mallGood);
            mallGoodMapper.updateCategory(mallGood);
//            if(i == 1) {
//                mallGoodMapper.deleteByGoodId(mallGood.getId());
//            }
        } else {
            mallGood.setGmtCreate(date);
            mallGood.setAuditStatus((byte)CommonEnum.AuditStatus.Waiting.getType());
            if(mallGoodInDTO.getPicture().contains(",")){
                mallGood.setIconPicture(mallGoodInDTO.getPicture().substring(0, mallGoodInDTO.getPicture().indexOf(",")));
            }else{
                mallGood.setIconPicture(mallGoodInDTO.getPicture());
            }
            mallGood.setCanUsedShop(mallGoodInDTO.getMshopId().toString());
            i = mallGoodMapper.insertSelective(mallGood);
        }

        baseOutDTO.setErrorCode(Rescode.SUCCESS);
        return baseOutDTO;
    }




    @Override
    public void updateEntityGood(List<MallGoodUpdateDTO> mallGoodUpdateDTOList) throws Exception {
        for(int i = 0; i < mallGoodUpdateDTOList.size(); i++) {
            MallEntityGood mallEntityGood = new MallEntityGood();
            PropertyCopyUtil.copyProperties(mallGoodUpdateDTOList.get(i), mallEntityGood);
            if(mallEntityGood.getCategoryFirstId() != null) {
                mallEntityGoodMapper.updateCategory(mallEntityGood);
            } else {
                mallEntityGoodMapper.updateByPrimaryKeySelective1(mallEntityGood);
            }
        }
    }

    /**
     * 逻辑删除商品和商品规格
     * @param items
     * @param groupId
     */
    @Override
    @Transactional
    public void delEntityGood(List<Long> items, Long groupId) {
       /* int i = mallEntityGoodMapper.delEntityGood(items, groupId);
        if(i == items.size()) {
            mallEntityGoodSpecMapper.deleteByGoodIds(items);
        }*/
        int i = mallEntityGoodMapper.logicDelEntityGood(items, groupId);
        if(i == items.size()) {
            mallEntityGoodSpecMapper.logicDelGoodSpec(items);
        }
    }

    @Override
    public EntityGoodInDTO getEntityGood(Long id, Long groupId) throws Exception {
        EntityGoodInDTO entityGoodInDTO = new EntityGoodInDTO();
        MallEntityGood mallEntityGood = mallEntityGoodMapper.selectByPrimaryKeyAndGroupId(id, groupId);
        PropertyCopyUtil.copyProperties(mallEntityGood, entityGoodInDTO);
        List<MallEntityGoodSpec> mallEntityGoodSpecList = mallEntityGoodSpecMapper.selectByGoodId(mallEntityGood.getId());

        if(entityGoodInDTO.getPostMethod() == -2) {
            entityGoodInDTO.setPostMethodStr("自提");
        } else if (entityGoodInDTO.getPostMethod() == -1) {
            entityGoodInDTO.setPostMethodStr("免运费");
        } else {
            entityGoodInDTO.setPostMethodStr(mallFreightMapper.selectByPrimaryKey(entityGoodInDTO.getPostMethod()).getName());
        }
        entityGoodInDTO.setMallEntityGoodSpecList(mallEntityGoodSpecList);
        return entityGoodInDTO;
    }

    @Override
    public List<Map<String, Object>> listFirstGoods(String memberId) {
        Long memberId1 = Long.parseLong(memberId);
        return mallEntityGoodMapper.listFirstGoods(memberId1);
    }

    @Override
    public List<Map<String, Object>> listHomeGoods(String memberId) {
        Long memberId1 = Long.parseLong(memberId);
        return mallEntityGoodMapper.listHomeGoods(memberId1);
    }

    
    @Override
    public List<Map<String, Object>> listGoods(String memberId, String page, String key, String categroyId) {
        Long memberId1 = Long.parseLong(memberId);
        Integer start = (Integer.parseInt(page) - 1) * 10;
        Long categoryId1 = null;
        if(categroyId != null) {
            categoryId1 = Long.parseLong(categroyId);
        }
        return mallEntityGoodMapper.listGoods(memberId1, key, start, categoryId1);
    }

    @Override
    @Transactional
    public Response addSubmitCart(String memberId, List<Long> goodIds, String addressId) {
        Member member = memberMapper.selectByPrimaryKey(Long.parseLong(memberId));

        List<MallCartBean> mallCartList = mallCartMapper.listByIdAndGoodIds(member.getId(), goodIds);
        int j = 0;
        int totalGoodNumber = 0;
        int totalGoodAmount = 0;
        Date date = new Date();
        try {
            for(int i = 0; i < mallCartList.size(); i++) {
                MallCart mallCart = mallCartList.get(i);
                j = i;
                mallEntityGoodMapper.reduceGoodStockNumber(mallCart.getGoodId(), mallCart.getGoodNumber());
                MallEntityGood mallEntityGood = mallEntityGoodMapper.selectByPrimaryKey(mallCart.getGoodId());
                if(mallEntityGood.getLimitNumber() > 0) {
                    long memberHavePurchaseNum = orderGoodItemMapper.countMemberHavePurchase(Long.parseLong(memberId), mallCart.getGoodId());
                    if(memberHavePurchaseNum + mallCart.getGoodNumber() > mallEntityGood.getLimitNumber()) {
                        return Response.getError("商品" + mallCartList.get(j).getName() + "超过最大购买数量，请重新选择商品");
                    }
                }

                totalGoodNumber += mallCart.getGoodNumber();
                totalGoodAmount += (mallCart.getPromotionPrice() == null ? mallCart.getSalePrice() : mallCart.getPromotionPrice()) * mallCart.getGoodNumber();
            }
        } catch (Exception e) {
            return Response.getError("商品" + mallCartList.get(j).getName() + "没有足够的库存，请重新选择商品");
        }

        int i = mallCartMapper.deleteByIdAndGoodIds(member.getId(), goodIds);

        OrderGood orderGood = new OrderGood();
        orderGood.setAddressId(Long.parseLong(addressId));
        orderGood.setGmtCreate(date);
        orderGood.setGmtModified(date);
        orderGood.setMemberId(member.getId());
        orderGood.setGroupId(member.getGroupId());
        orderGood.setGoodNum(totalGoodNumber);
        orderGood.setGoodAmount(totalGoodAmount);
        orderGood.setOrderNo(SysUtils.createOrderId(""));
        orderGood.setStatus(BizEnum.MallOrderStatus.WaitingForPay.getType());
        orderGoodMapper.insertSelective(orderGood);
        
        for(MallCartBean  mallCartBean: mallCartList){
        	 OrderGoodItem orderGoodItem = new OrderGoodItem();
             orderGoodItem.setOrderId(orderGood.getId());
             orderGoodItem.setGoodId(mallCartBean.getGoodId());
             orderGoodItem.setGoodName(mallCartBean.getName());
             orderGoodItem.setGoodNum(mallCartBean.getGoodNumber());
             orderGoodItem.setGoodsImage(mallCartBean.getIconUrl());
             orderGoodItem.setOriginalPrice(mallCartBean.getSalePrice());
             orderGoodItem.setPromotionPrice(mallCartBean.getPromotionPrice());
             orderGoodItem.setSettingPrice(mallCartBean.getSupplyPrice());
             orderGoodItem.setShopId(mallCartBean.getShopId());
             orderGoodItem.setIsSetted(false);
           
             orderGoodItemMapper.insertSelective(orderGoodItem);
        }
 
        TradeOffline tradeOffline=new TradeOffline();
	    tradeOffline.setGmtCreate(date);
	    tradeOffline.setGmtModified(date);
	    tradeOffline.setGroupId(member.getGroupId());
	    tradeOffline.setOrderId(orderGood.getOrderNo());
	    tradeOffline.setPayerId(Long.parseLong(memberId));//付款者id号
	    tradeOffline.setPayerName(member.getName());//付款者姓名
        tradeOffline.setPayerType((byte) CommonEnum.UserType.User.getType());
	    tradeOffline.setPayType((byte) BizEnum.PaymentType.Other.getType());//支付方式
	    tradeOffline.setReceiverId(null);//接收者id
	    tradeOffline.setReceiverName(null);//接收者 此处以核销录入数据
	    tradeOffline.setTransactionType((byte)BizEnum.TransactionType.Consumption.getType());//交易类型
	    tradeOffline.setSubType((byte)BizEnum.PaymentScene.Online.getType());//支付场景
	    tradeOffline.setOrderAmount(totalGoodAmount); //订单金额
	    tradeOffline.setDiscountAmount(0);//折扣金额
	    tradeOffline.setRealAmount(totalGoodAmount);//实付金额
	    tradeOffline.setRelatedActivityId(null);//关联活动
	    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.WaitingForPay.getType());//交易状态
	    tradeOffline.setIsWallet(false);
	    tradeOffline.setGoodType(null);//商品类型(线上用) ？
	    tradeOffline.setMemo("商品购买");
	    tradeOffline.setDetail("用户商品购买");
	    tradeOffline.setMerchantNo(null);//商户号(对账用)
	    tradeOffline.setTerminalNo(null);//商户终端号
	    tradeOffline.setPayAccount("");//支付卡号
	    tradeOffline.setScanOrganization((byte)BizEnum.ScanInNetCompany.fuqianla.getType());//扫码付入网机构
	    tradeOffline.setCrcodeStr(null); 
	    tradeOffline.setOldOrderId(null); 
	    tradeOffline.setDividableAmount(0);//可分润金额
	    tradeOffline.setAttachStatus(BizEnum.ProfitCalculateStatus.NoDeal.getType());
	    tradeOffline.setPayOrderid(null);
	    tradeOffline.setPayBakOrderid(null);
	    tradeOffline.setInNetCompany(null);//入网机构
	    tradeOfflineMapper.insertSelective(tradeOffline);
        return Response.getSuccess(orderGood.getOrderNo(),"订单创建成功");
    }

    @Override
    public Response addDirectBuy(String memberId, String goodsId, String goodNum) {
        Long goodId = Long.parseLong(goodsId);
        int goodsNum = Integer.parseInt(goodNum);
        Member member = memberMapper.selectByPrimaryKey(Long.parseLong(memberId));
        MemberAddress memberAddress = memberAddressMapper.selectDefaultRecordByMemberId(Long.parseLong(memberId));
        if(memberAddress == null) {
            return Response.getError("未获取到用户默认地址");
        }

        MallEntityGood mallEntityGood = mallEntityGoodMapper.selectByPrimaryKey(goodId);

        if(mallEntityGood.getLimitNumber() > 0) {
            long memberHavePurchaseNum = orderGoodItemMapper.countMemberHavePurchase(Long.parseLong(memberId), goodId);
            if(memberHavePurchaseNum + goodsNum > mallEntityGood.getLimitNumber()) {
                return Response.getError("超过商品限制最大购买数量");
            }
        }

        try {
            mallEntityGoodMapper.reduceGoodStockNumber(goodId, goodsNum);
//            Integer goodStock = mallEntityGoodMapper.selectGoodStockById(goodId);
//            if(goodStock == 0) {
//                mallEntityGoodMapper.updateSoldOutGoodById(goodId);
//            }
            mallEntityGoodMapper.updateUnlaunchGood(goodId);
        } catch (Exception e) {
            return Response.getError("商品库存不足");
        }

        int totalGoodAmount = (mallEntityGood.getPromotionPrice() == null ? mallEntityGood.getSalePrice() : mallEntityGood.getPromotionPrice()) * goodsNum;

        Date date = new Date();
        OrderGood orderGood = new OrderGood();
        orderGood.setAddressId(memberAddress.getId());
        orderGood.setGmtCreate(date);
        orderGood.setGmtModified(date);
        orderGood.setMemberId(member.getId());
        orderGood.setGroupId(member.getGroupId());
        orderGood.setGoodNum(goodsNum);
        orderGood.setGoodAmount(totalGoodAmount);
        orderGood.setOrderNo(SysUtils.createOrderId(""));
        orderGood.setStatus(BizEnum.MallOrderStatus.WaitingForPay.getType());
        orderGoodMapper.insertSelective(orderGood);

        OrderGoodItem orderGoodItem = new OrderGoodItem();
        orderGoodItem.setOrderId(orderGood.getId());
        orderGoodItem.setGoodId(mallEntityGood.getId());
        orderGoodItem.setGoodName(mallEntityGood.getName());
        orderGoodItem.setGoodNum(goodsNum);
        orderGoodItem.setGoodsImage(mallEntityGood.getPicture().split(",")[0]);
        orderGoodItem.setOriginalPrice(mallEntityGood.getSalePrice());
        orderGoodItem.setPromotionPrice(mallEntityGood.getPromotionPrice());
        orderGoodItem.setSettingPrice(mallEntityGood.getSupplyPrice());
        orderGoodItem.setShopId(mallEntityGood.getShopId());
        orderGoodItem.setIsSetted(false);
        orderGoodItemMapper.insertSelective(orderGoodItem);

        TradeOffline tradeOffline=new TradeOffline();
	    tradeOffline.setGmtCreate(date);
	    tradeOffline.setGmtModified(date);
	    tradeOffline.setGroupId(member.getGroupId());
	    tradeOffline.setOrderId(orderGood.getOrderNo());
	    tradeOffline.setPayerId(Long.parseLong(memberId));//付款者id号
	    tradeOffline.setPayerName(member.getName());//付款者姓名
        tradeOffline.setPayerType((byte)CommonEnum.UserType.User.getType());
        tradeOffline.setPayType((byte) BizEnum.PaymentType.Other.getType());//支付方式
	    tradeOffline.setReceiverId(null);//接收者id
	    tradeOffline.setReceiverName(null);//接收者 此处以核销录入数据
	    tradeOffline.setTransactionType((byte)BizEnum.TransactionType.Consumption.getType());//交易类型
	    tradeOffline.setSubType((byte)BizEnum.PaymentScene.Online.getType());//支付场景
	    tradeOffline.setOrderAmount(totalGoodAmount); //订单金额
	    tradeOffline.setDiscountAmount(0);//折扣金额
	    tradeOffline.setRealAmount(totalGoodAmount);//实付金额
	    tradeOffline.setRelatedActivityId(null);//关联活动
	    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.WaitingForPay.getType());//交易状态
	    tradeOffline.setIsWallet(false);
	    tradeOffline.setGoodType(null);//商品类型(线上用) ？
	    tradeOffline.setMemo("商品购买");
	    tradeOffline.setDetail("用户商品购买");
	    tradeOffline.setMerchantNo(null);//商户号(对账用)
	    tradeOffline.setTerminalNo(null);//商户终端号
	    tradeOffline.setPayAccount("");//支付卡号
	    tradeOffline.setScanOrganization((byte)BizEnum.ScanInNetCompany.fuqianla.getType());//扫码付入网机构
	    tradeOffline.setCrcodeStr(null); 
	    tradeOffline.setOldOrderId(null); 
	    tradeOffline.setDividableAmount(0);//可分润金额
	    tradeOffline.setAttachStatus(BizEnum.ProfitCalculateStatus.NoDeal.getType());
	    tradeOffline.setPayOrderid(null);
	    tradeOffline.setPayBakOrderid(null);
	    tradeOffline.setInNetCompany(null);//入网机构
	    tradeOfflineMapper.insertSelective(tradeOffline);
        return Response.getSuccess(orderGood.getOrderNo(),"订单创建成功");
    }

    @Override
    public List<OrderGood> goodsOrders(String memberId, String page, String status) {
        Integer status1 = null;
        if(status != null && !status.equals("")) {
            status1 = Integer.parseInt(status);
        }
        Integer start = (Integer.parseInt(page) - 1) * 10;
        return orderGoodMapper.listGoodsOrders(Long.parseLong(memberId), start, status1);
    }

    @Override
    public Map<String, Object> getGoodsItem(Long goodsId1) {
        Map<String, Object> goodInfo = mallEntityGoodMapper.selectByGoodId(goodsId1);
        if(goodInfo.get("goodsImage") != null) {
            String[] imgStr = ((String) goodInfo.get("goodsImage")).split(",");
            for(int i = 0; i < imgStr.length; i++) {
                if(i == 0) {
                    goodInfo.put("goodsImage", imgStr[i]);
                } else {
                    goodInfo.put("goodsImage" + (i + 1), imgStr[i]);
                }
            }
        }

        String jsonStr = (String)goodInfo.get("goodsAttrs");
        if(jsonStr != null) {
            Map<String, String> goodsAttrs = JSON.parseObject(jsonStr, Map.class);
            List<Map<String, String>> goodsAttrsList = new ArrayList<>();
            for(Map.Entry<String, String> entry : goodsAttrs.entrySet()) {
                Map<String, String> goodAttrs = new HashMap<>();
                goodAttrs.put("goodsAttrsName", entry.getKey());
                goodAttrs.put("goodsAttrsValue", entry.getValue());
                goodsAttrsList.add(goodAttrs);
            }
            goodInfo.put("goodsAttrs", goodsAttrsList);
        }
        return goodInfo;
    }

    @Override
    public OrderGood getGoodsOrder(Long memberId1, Long orderId) {
        return orderGoodMapper.getGoodsOrder(memberId1, orderId);
    }

    @Override
    public Response saveOrderCancel(Long memberId, Long orderId,String masterUrl) {
        OrderGood orderGood = orderGoodMapper.selectByPrimaryKey(orderId);
        if(orderGood == null) {
            return null;
        }
        int i = orderGoodMapper.orderCancel(memberId, orderId);
        if(i == 1) {
            //商品加库存
            List<OrderGoodItem> orderGoodItemList = orderGoodItemMapper.listByOrderId(orderId);
            for(int j = 0; j < orderGoodItemList.size(); j++) {
                //取消订单，加库存，但不减销量
                mallEntityGoodMapper.addGoodStockNumber(orderGoodItemList.get(j));
            }
            //解锁优惠券
            if(orderGood.getCouponCover() > 0) {
                couponUserMapper.updateUnlockCouponOnMemberByOrderId(orderId);
                couponUserMapper.updateUnlockCouponOnNoMemberByOrderId(orderId);
            }

            //解锁积分
            if(orderGood.getBonusAmount() > 0) {
                bonusVerifyLogMapper.updateStatusByOrderId(orderGood.getId(), ActivityEnum.BonusVerityStatus.OrderCancel.getType());
                memberMapper.updateMemberBonusUnlock(memberId, orderGood.getBonusAmount());
            }
            
            try {
				templateMailSendService.sendOrderTemplate(orderGood.getOrderNo(), BizEnum.WebTemplateType.template12.getType(), memberId, orderGood.getGroupId(), masterUrl);
			} catch (Exception e) {
				e.printStackTrace();
			}
            return Response.buildSuccess("取消成功");
        }
        return Response.getError("操作失败");
    }

    @Override
    public Response saveOrderDel(Long memberId, Long orderId) {
        int i = orderGoodMapper.markAsDel(memberId, orderId);
        if(i == 1) {
            return Response.buildSuccess("删除成功");
        }
        return Response.getError("操作失败");
    }


    /**
     * 会员优惠券 按商品 分享获取优惠券
     * @param mallGoodSelectiveQueryDTO
     * @return
     * @throws Exception
     */
    @Override
    public MallGoodSelectiveListOutDTO shareGoodSelective(MallGoodSelectiveQueryDTO mallGoodSelectiveQueryDTO) throws Exception {
        MallGoodSelectiveListOutDTO mallGoodSelectiveListOutDTO = new MallGoodSelectiveListOutDTO();
        List<MallGoodSelectiveOutDTO> mallGoodSelectiveOutDTOList = new ArrayList<MallGoodSelectiveOutDTO>();

//        Long total;
//        List<MallGood> mallGoodList;
        //待修改  这里要的是服务商品跟商城商品的


        /*if(mallGoodSelectiveQueryDTO.getGoodType() == CommonEnum.GoodType.Service.getType()) {
            total = mallGoodMapper.countGoodSelective(mallGoodSelectiveQueryDTO);
            mallGoodList = mallGoodMapper.goodSelective(mallGoodSelectiveQueryDTO);
        } else {
            total = mallEntityGoodMapper.countGoodSelective(mallGoodSelectiveQueryDTO);
            mallGoodList = mallEntityGoodMapper.goodSelective(mallGoodSelectiveQueryDTO);   //这里为了方便  强行使用MallGood作为商城商品的返回值
        }
*/
        Long total = mallGoodMapper.countMallGoodSelective(mallGoodSelectiveQueryDTO);
        List<MallGood> mallGoodList = mallGoodMapper.shareMallGoodSelective(mallGoodSelectiveQueryDTO);

        Long total2 = mallEntityGoodMapper.countEntityGoodSelective(mallGoodSelectiveQueryDTO);
        List<MallGood> mallGoodList2 = mallEntityGoodMapper.shareEntityGoodSelective(mallGoodSelectiveQueryDTO);

        mallGoodList.addAll(mallGoodList2);
        total+=total2;

        for(int i = 0; i < mallGoodList.size(); i++) {
            MallGoodSelectiveOutDTO mallGoodSelectiveOutDTO = new MallGoodSelectiveOutDTO();
            PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodSelectiveOutDTO);
            mallGoodSelectiveOutDTOList.add(mallGoodSelectiveOutDTO);
        }
        mallGoodSelectiveListOutDTO.setTotal(total);
        mallGoodSelectiveListOutDTO.setMallGoodSelectiveOutDTOList(mallGoodSelectiveOutDTOList);
        return mallGoodSelectiveListOutDTO;
    }
    @Override
    public List<Long> getShareGoodIdByCategoryId(Map<String, List<Long>> items) {
        List<Long> ids = new ArrayList<Long>();
        if(items.get("categoryOneLevelIds").size() > 0) {
            ids.addAll(mallGoodMapper.shareListIdByOneLevelCatetoryId(items.get("categoryOneLevelIds")));

        }
        if(items.get("categoryTwoLevelIds").size() > 0) {
            ids.addAll(mallGoodMapper.shareListIdByTwoLevelCatetoryId(items.get("categoryTwoLevelIds")));
        }
        return ids;
    }


    @Override
    public BaseOutDTO checkStatusSuccess(MallGoodQueryDTO mallGoodQueryDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        MallEntityGood mallEntityGood = new MallEntityGood();
        mallEntityGood.setId(mallGoodQueryDTO.getId());
        mallEntityGood.setGmtModified(new Date());
        mallEntityGood.setCheckStatus((int)mallGoodQueryDTO.getAuditStatus());
        mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallEntityGood.setRemark(mallGoodQueryDTO.getRemark());


        if(mallGoodQueryDTO.getShopId()!=null) {//如果是商户商品，则进行推送
            //获取商户账号
            Userbase userbase = shopMapper.getUserNameByShopId(mallGoodQueryDTO.getShopId());
            //发送退款推送信息
            Notice notice = new Notice();
            try {
                notice.setNoticetype(0);
                notice.setSender(mallGoodQueryDTO.getGroupId());
                notice.setReceiver(userbase.getUsername());
                notice.setNoticetitle("商品上架通知");
                notice.setNoticetarget("ACCOUNT");
                if((int)mallGoodQueryDTO.getAuditStatus()==1){
                    notice.setNoticebody("您的产品已审核通过,请详情点击查看");
                }else{
                    notice.setNoticebody("您的产品已审核未通过,请详情点击查看");
                }
                notice.setGroupid(mallGoodQueryDTO.getGroupId());
                notice.setNoticestatus(1);
                notice.setIsplay(1);
                notice.setType(3);
                notice.setTvalue("");
                appNoticeService.sendNoticeToUsers(notice);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        mallEntityGoodMapper.updateByPrimaryKeySelective(mallEntityGood);
        baseOutDTO.setErrorCode(Rescode.SUCCESS);

        return baseOutDTO;
    }

    @Override
    public Response getEntityGoodByName(MallEntityGood mallEntityGood) throws Exception {
        try {
          List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.getEntityGoodsByName(mallEntityGood.getGroupId(),mallEntityGood.getName());
            if(mallEntityGoodList!=null && mallEntityGoodList.size()>0){
                return Response.getSuccess(mallEntityGoodList);
            }else{
                return Response.getError("未查询到该商品");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.getError("商品查询系统异常");
        }
    }

    @Override
    public Response getServiceGoodByName(MallGood mallGood) throws Exception {
        try {
            List<MallGood> mallGoodList = mallGoodMapper.getMallGoodByName(mallGood);
            if (mallGoodList!=null && mallGoodList.size()>0) {
                return Response.getSuccess(mallGoodList);
            }else{
                return Response.getError("未查询到该商品");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.getError("商品查询系统异常");
        }
    }

    @Override
    public MallGoodListOutDTO shopEntityGoodList(MallGoodQueryDTO mallGoodQueryDTO, Page page) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
        ShopSelectQueryDTO shopSelectQueryDTO = new ShopSelectQueryDTO();

        shopSelectQueryDTO.setGroupId(mallGoodQueryDTO.getGroupId());
        shopSelectQueryDTO.setAgentId(mallGoodQueryDTO.getAgentId());

        List<Long> shopList = shopService.getAgentShopList(shopSelectQueryDTO);//根据渠道ID，代理商ID，查询该代理商的直属门店信息

        //代理商直属门店查询成功
        if(shopList != null && shopList.size()>0){

            MallEntityGood mallEntityGood = new MallEntityGood();
            mallEntityGood.setName(mallGoodQueryDTO.getName());
            mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
            mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
            mallEntityGood.setGroupId(mallGoodQueryDTO.getGroupId());
            mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
            mallEntityGood.setPage(page);


            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            String dateStart = mallGoodQueryDTO.getDateStart();
            String dateEnd = mallGoodQueryDTO.getDateEnd();
            if(!Tools.isEmpty(dateStart)) {
                Date dateStartDate = dateFormat.parse(dateStart);
                mallEntityGood.setDateStart(dateStartDate);
            }
            if(!Tools.isEmpty(dateEnd)) {
                Date dateEndDate = dateFormat.parse(dateEnd);
                mallEntityGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
            }

            List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.getAgentShopEntityGoodList(shopList,mallEntityGood);

            List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
            for(int i = 0; i < mallEntityGoodList.size(); i++) {
                MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
                PropertyCopyUtil.copyProperties(mallEntityGoodList.get(i), mallGoodOutDTO);

                Map stockNumber=orderGoodMapper.selectAllStockNumberByGoodId(mallGoodOutDTO.getId());
                if(stockNumber==null){
                    mallGoodOutDTO.setAllStockNumber(mallGoodOutDTO.getStockNumber()+mallGoodOutDTO.getSaleNum());
                }else{
                    mallGoodOutDTO.setAllStockNumber(Integer.parseInt(stockNumber.get("allStockNumber").toString())+mallGoodOutDTO.getStockNumber());
                }

                mallGoodOutDTOList.add(mallGoodOutDTO);
            }

            mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
            mallGoodListOutDTO.setPage(page);
            mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);
        }else{
           //查询代理商直属门店异常，或该代理商没有直属门店

        }
        return mallGoodListOutDTO;
    }

}
